package com.chixing.service.impl;



import com.chixing.mapper.CommentMapper;
import com.chixing.mapper.GoodMapper;
import com.chixing.pojo.Comment;
import com.chixing.pojo.Good;
import com.chixing.service.PraiseService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

@Configuration
@MapperScan("com.chixing.mapper")
public class PraiseServiceImpl implements PraiseService {
  @Autowired
  private RedisTemplate redisTemplate;

  @Autowired
  private GoodMapper goodMapper;

  @Autowired
  private CommentMapper commentMapper;

  @Scheduled(cron = "0/5 * * * * ?")
  @Override
  public void transRedisDataSave2DB() {

    //1. 获得redis中的用户点赞记录，保存到mysql
    //entries获得redis中的键值对。
    HashMap<String, Integer> customer_praise_record = (HashMap<String, Integer>) redisTemplate.opsForHash().entries("save_praise_record");
    ArrayList<Good> goods = new ArrayList<>();
    Iterator<Map.Entry<String, Integer>> iterator = customer_praise_record.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<String, Integer> next = iterator.next();
      //用户id::评论id 点赞状态。
      String key = next.getKey();
      String[] split = key.split("::");
      //用户id
      Integer cusId = Integer.valueOf(split[0]);
      //评论id
      Integer commendId = Integer.valueOf(split[1]);
      //点赞状态。
      Integer value = next.getValue();

      Good good = new Good();
      good.setCommentId(commendId);
      good.setCustomerId(cusId);
      //点赞时间
      good.setGoodTime(new Date());
      good.setGoodStatus(value);
      goods.add(good);
    }

    if (goods.size() != 0) {
      //批量插入点赞信息。
      goodMapper.insertBatch(goods);
    }

    redisTemplate.delete("save_praise_record");

    //2.获得redis中的评论点赞总数，修改到mysql中。
    HashMap<String, Integer> comment_praise_count = (HashMap<String, Integer>) redisTemplate.opsForHash().entries("comment_praise_count");
    Iterator<Map.Entry<String, Integer>> iterator1 = comment_praise_count.entrySet().iterator();
    while (iterator1.hasNext()) {
      Map.Entry<String, Integer> next = iterator1.next();

      //评论id
      String key = next.getKey();
      System.out.println("---评论id---" + key);

      //评论点赞总数
      Integer value = next.getValue();
      System.out.println("---点赞总数---" + value);

      Comment comment = new Comment();
      comment.setCommentId(Integer.valueOf(key));
      comment.setGoodNumber(value);

      commentMapper.updateByPrimaryKeySelective(comment);

    }
  }

  @Scheduled(cron = "0/5 * * * * ?")
  @Override
  public void canclePraiseFromRedisDataSave2DB() {

    //1. 获得redis中的用户点赞记录，保存到mysql
    HashMap<String, Integer> customer_praise_record = (HashMap<String, Integer>) redisTemplate.opsForHash().entries("cancle_praise_record");
    ArrayList<Good> goods = new ArrayList<>();
    Iterator<Map.Entry<String, Integer>> iterator = customer_praise_record.entrySet().iterator();
    while (iterator.hasNext()) {
      Map.Entry<String, Integer> next = iterator.next();
      //用户id::评论id 点赞状态。
      String key = next.getKey();
      String[] split = key.split("::");
      //用户id
      Integer cusId = Integer.valueOf(split[0]);
      //评论id
      Integer commendId = Integer.valueOf(split[1]);
      //点赞状态。
      Integer value = next.getValue();

      Good good = new Good();
      good.setCommentId(commendId);
      good.setCustomerId(cusId);
      //点赞时间
      good.setGoodTime(new Date());
      good.setGoodStatus(value);
      goods.add(good);
      System.out.println(good);
    }
    if (goods.size() != 0) {
      //批量插入点赞信息。
      goodMapper.updateBatch(goods);
    }

    redisTemplate.delete("cancle_praise_record");

    //2.获得redis中的评论点赞总数，修改到mysql中。
    HashMap<String, Integer> comment_praise_count = (HashMap<String, Integer>) redisTemplate.opsForHash().entries("comment_praise_count");
    Iterator<Map.Entry<String, Integer>> iterator1 = comment_praise_count.entrySet().iterator();
    while (iterator1.hasNext()) {
      Map.Entry<String, Integer> next = iterator1.next();

      //评论id
      String key = next.getKey();
      System.out.println("---评论id---" + key);

      //评论点赞总数
      Integer value = next.getValue();
      System.out.println("---点赞总数---" + value);

      Comment comment = new Comment();
      comment.setCommentId(Integer.valueOf(key));
      comment.setGoodNumber(value);

      commentMapper.updateByPrimaryKeySelective(comment);
    }
  }
}
